const nodemailer = require('nodemailer');

//设置邮箱配置
let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',//邮箱服务的主机，如smtp.qq.com
    port: '465',//对应的端口号 465 587
    //开启安全连接
    secure: true,
    secureConnection:true,
    //用户信息
    auth: {
        user: '695166116@qq.com',
        pass: 'satnbljeopusbdfj'
    }
});

//设置收件人信息
let mailOptions = {
    from: '',//谁发的
    to: '',//发给谁
    subject: '',//主题是什么
    text: '',//文本内容
    html: '',//html模板

    //附件信息
    attachments: [
        {
            filename: '',
            path: '',
        }
    ]
};

// //发送邮件
// transporter.sendMail(mailOptions, (error, info) => {
//     if (error)
//         return console.log(error);
//     console.log(`Message: ${info.messageId}`);
//     console.log(`sent: ${info.response}`);
// });

const object = {
    sendMail: (to, subject, from, html)=>{
        let mailOptions = {
            from: from + '<695166116@qq.com>',//谁发的
            to,//发给谁
            subject,//主题是什么
            html//html模板
        };

        //发送邮件
        transporter.sendMail(mailOptions, (error, info) => {
            if (error)
                return console.log(error);
            console.log(`Message: ${info.messageId}`);
            console.log(`sent: ${info.response}`);
        });
    }
};

module.exports = object;

// try {
//     console.log("start");
//     object.sendMail('18768122418@163.com', 'node test', 'csy', '123123');
//     console.log("end");
// } catch (e) {
//     console.error(e);
// }